package com.base.cn.platform.os.controller.manage.function;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.function.SysFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源权限管理Controller
 *
 * @author s.li
 * @create 2018-03-14 17:55
 */
@Controller
@RequestMapping("/admin/manage/function")
public class AdminSysFunctionController extends AdminBaseController {

    @Autowired
    private SysFunctionService sysFunctionService;

    /**
     * 初始权限管理页面
     * @return
     */
    @RequestMapping("/toSysFunction")
    public String toSysFunction(){
        return resultPage("/manage/function/sys_function");
    }

    /**
     * 加载所有的系统权限路径
     * @return Map<String,Object>
     */
    @RequestMapping("/ajax/listSysFunction")
    @ResponseBody
    public Map<String,Object> listSysFunction(HttpServletRequest request){
        Map<String,Object> params = new HashMap<String,Object>();
        List<Map<String,Object>> sysFunctionList = sysFunctionService.findSysFunctionList(params);
        return ResultUtil.SUCCESS("权限查询成功",sysFunctionList);
    }

    /**
     * 初始化修改/添加权限页面
     * @param model
     * @param parentId 父级节点ID
     * @param functionId 权限ID（如果是修改就传，如果是添加可不传）
     * @param level 要操作节点的级别 （0，1，2）
     * @return 编辑页面
     */
    @RequestMapping(value = {"/initUpdateFunction","/initAddFunction"})
    public String initUpdateOrAddFunction(Model model,
                                          @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId,
                                          @RequestParam(value = "functionId",required = false,defaultValue = "0") BigDecimal functionId,
                                          @RequestParam(value = "level",defaultValue = "0") int level){
        Map<String,Object> sysFunction = new HashMap<String,Object>();
        if(functionId.compareTo(new BigDecimal(0))==1){
            sysFunction = sysFunctionService.findSysFunctionById(functionId);
        }else{
            sysFunction.put("parentId",parentId);
        }
        model.addAttribute("sysFunction",sysFunction);
        model.addAttribute("level",level);
        return resultPage("/manage/function/sys_function_edit");
    }

    /**
     * 新增或更新系统权限
     */
    @RequestMapping("/ajax/saveSysFunction")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "新增或更新系统权限")
    public Map<String, Object> saveSysFunction(@RequestBody Map<String,Object> sysFunction) {
        Map<String,Object> resultMap = sysFunctionService.saveSysFunction(sysFunction);
        if((boolean)resultMap.get("success")==true && (StringUtils.isEmpty((String)sysFunction.get("id")) || new BigDecimal(sysFunction.get("id").toString()).compareTo(new BigDecimal("0")) == 0)){
            String strParentId = (String)sysFunction.get("parentId");
            if(StringUtils.isNotEmpty(strParentId)){
                BigDecimal parentId = new BigDecimal(strParentId);
                resultMap.put("parentId",parentId);
            }
        }
        return resultMap;
    }

    /**
     * 删除权限
     * @param ids 要删除权限的ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/ajax/deleteSysFunction")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "删除系统权限")
    public Map<String,Object> deleteSysFunction(String ids){
        if(StringUtils.isNotEmpty(ids)){
            ids = StringUtils.subHeadTailString(ids,",");
            if(StringUtils.isNotEmpty(ids)){
                return sysFunctionService.deleteSysFunction(ids);
            }
        }
        return ResultUtil.SUCCESS("权限删除成功");
    }

    /**
     * 修改节点的父级关系
     * @param functionId 要修改的节点ID
     * @param parentId 新的父级ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/ajax/updateSysFunctionParent")
    @ResponseBody
    public Map<String,Object> updateSysFunctionParent(BigDecimal functionId,BigDecimal parentId){
        return sysFunctionService.updateSysFunctionParent(functionId,parentId);
    }
}
